package com.wuyou.sso2.config;

import com.wuyou.sso2.component.CustomizedTokenServices;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenStore;

/**
 * 认证服务器配置
 *
 * @author lishangbu
 * @date 2018/6/22
 */
@Configuration
@RequiredArgsConstructor
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    private final ClientDetailsService cacheableClientDetailsService;

    private final AuthenticationManager authenticationManagerBean;

    private final TokenStore customizedRedisTokenStore;

    private final UserDetailsService customizedUserDetailsService;

    private final TokenEnhancer customizedTokenEnhancer;

//    private final AuthProperties authProperties;

    @Override
    @SneakyThrows
    public void configure(ClientDetailsServiceConfigurer clients) {
        clients.withClientDetails(cacheableClientDetailsService);
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer oauthServer) {
        oauthServer.allowFormAuthenticationForClients().checkTokenAccess("isAuthenticated()");
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
        endpoints
                .allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST)
                .tokenStore(customizedRedisTokenStore)
                .tokenEnhancer(customizedTokenEnhancer)
                .userDetailsService(customizedUserDetailsService)
                .authenticationManager(authenticationManagerBean)
//                .tokenServices(customizedTokenServices())
                .reuseRefreshTokens(false)
                .pathMapping("/oauth/confirm_access", "/token/confirm_access");
//                .exceptionTranslator(new CustomizedWebResponseExceptionTranslator());
    }

    /**
     * 自定义令牌服务
     *
     * @return
     */
    @Bean
    public AuthorizationServerTokenServices customizedTokenServices() {
        CustomizedTokenServices customizedTokenServices = new CustomizedTokenServices();
        customizedTokenServices.setTokenEnhancer(customizedTokenEnhancer);
        customizedTokenServices.setTokenStore(customizedRedisTokenStore);
        customizedTokenServices.setAuthenticationManager(authenticationManagerBean);
        customizedTokenServices.setClientDetailsService(cacheableClientDetailsService);
//        customizedTokenServices.setAuthProperties(authProperties);
        return customizedTokenServices;
    }
}
